package com.inventory.controller;

import com.inventory.utils.ResponseResult;
import com.inventory.entity.Inventory;
import com.inventory.service.InventoryService;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 库存信息 前后端交互类
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/api/inventory")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    /**
     * 添加库存信息
     */
    @PostMapping("/add.do")
    @CircuitBreaker(name = "inventoryService", fallbackMethod = "fallbackAddInventoryInfo")
    public ResponseResult addInventoryInfo(@RequestBody Inventory inventory) {
        if (inventoryService.doAddInventoryInfo(inventory)) {
            return ResponseResult.success("添加库存信息成功！");
        } else {
            return ResponseResult.failure("添加库存信息失败，可能是当前库存信息已存在！");
        }
    }

    public ResponseResult fallbackAddInventoryInfo(@RequestBody Inventory inventory, Throwable throwable) {
        System.err.println("断路器触发 - 添加库存信息失败: " + throwable.getMessage());
        return ResponseResult.failure("系统繁忙，请稍后再试");
    }

    /**
     * 修改库存信息
     */
    @PostMapping("/modify.do")
    @CircuitBreaker(name = "inventoryService", fallbackMethod = "fallbackModifyInventoryInfo")
    public ResponseResult modifyInventoryInfo(@RequestBody Inventory inventory) {
        if (inventoryService.doModifyInventoryInfo(inventory)) {
            return ResponseResult.success("修改库存信息成功！");
        } else {
            return ResponseResult.failure("修改库存信息失败，可能是当前库存信息不存在！");
        }
    }

    public ResponseResult fallbackModifyInventoryInfo(@RequestBody Inventory inventory, Throwable throwable) {
        System.err.println("断路器触发 - 修改库存信息失败: " + throwable.getMessage());
        return ResponseResult.failure("系统繁忙，请稍后再试");
    }

    /**
     * 丢弃库存信息（逻辑删除）
     */
    @PostMapping("/clear.do")
    @CircuitBreaker(name = "inventoryService", fallbackMethod = "fallbackClearInventoryInfo")
    public ResponseResult clearInventoryInfo(@RequestBody Inventory inventory) {
        Inventory existingInventory = inventoryService.doGetInventoryInfo(inventory);
        if (existingInventory == null) {
            return ResponseResult.failure("丢弃库存失败，库存信息不存在！");
        }

        // 逻辑删除：设置库存为0，保质期清空
        existingInventory.setExptime("-");
        existingInventory.setCount(0);

        if (inventoryService.doModifyInventoryInfo(existingInventory)) {
            return ResponseResult.success("丢弃库存成功！");
        } else {
            return ResponseResult.failure("丢弃库存失败！");
        }
    }

    public ResponseResult fallbackClearInventoryInfo(@RequestBody Inventory inventory, Throwable throwable) {
        System.err.println("断路器触发 - 丢弃库存信息失败: " + throwable.getMessage());
        return ResponseResult.failure("系统繁忙，请稍后再试");
    }

    /**
     * 采购库存信息（增加库存数量）
     */
    @PostMapping("/purchase.do")
    @CircuitBreaker(name = "inventoryService", fallbackMethod = "fallbackPurchaseInventoryInfo")
    public ResponseResult purchaseInventoryInfo(@RequestBody Inventory inventory) {
        Inventory existingInventory = inventoryService.doGetInventoryInfo(inventory);
        if (existingInventory == null) {
            return ResponseResult.failure("采购失败，库存信息不存在！");
        }

        // 增加库存数量
        existingInventory.setExptime(inventory.getExptime());
        existingInventory.setCount(existingInventory.getCount() + inventory.getCount());

        if (inventoryService.doModifyInventoryInfo(existingInventory)) {
            return ResponseResult.success("采购成功！");
        } else {
            return ResponseResult.failure("采购失败！");
        }
    }

    public ResponseResult fallbackPurchaseInventoryInfo(@RequestBody Inventory inventory, Throwable throwable) {
        System.err.println("断路器触发 - 采购库存信息失败: " + throwable.getMessage());
        return ResponseResult.failure("系统繁忙，请稍后再试");
    }

    /**
     * 删除库存信息（物理删除）
     */
    @PostMapping("/delete.do")
    @CircuitBreaker(name = "inventoryService", fallbackMethod = "fallbackDeleteInventoryInfo")
    public ResponseResult deleteInventoryInfo(@RequestBody Inventory inventory) {
        if (inventoryService.doDeleteInventoryInfo(inventory)) {
            return ResponseResult.success("删除库存信息成功！");
        } else {
            return ResponseResult.failure("删除库存信息失败，可能是当前库存信息不存在！");
        }
    }

    public ResponseResult fallbackDeleteInventoryInfo(@RequestBody Inventory inventory, Throwable throwable) {
        System.err.println("断路器触发 - 删除库存信息失败: " + throwable.getMessage());
        return ResponseResult.failure("系统繁忙，请稍后再试");
    }

    /**
     * 获取单个库存信息
     */
    @GetMapping("/get.do")
    @CircuitBreaker(name = "inventoryService", fallbackMethod = "fallbackGetInventoryInfo")
    public ResponseResult getInventoryInfo(Inventory inventory) {
        Inventory result = inventoryService.doGetInventoryInfo(inventory);
        if (result != null) {
            return ResponseResult.success("获取库存信息成功！", result);
        } else {
            return ResponseResult.failure("获取库存信息失败，库存不存在！");
        }
    }

    public ResponseResult fallbackGetInventoryInfo(Inventory inventory, Throwable throwable) {
        System.err.println("断路器触发 - 获取库存信息失败: " + throwable.getMessage());
        // 修正：使用 success 传递临时空对象，或调整为 failure 仅传文案
        return ResponseResult.success("系统繁忙，返回临时数据", new Inventory());
    }

    /**
     * 获取库存列表
     */
    @GetMapping("/list.do")
    @CircuitBreaker(name = "inventoryService", fallbackMethod = "fallbackGetInventoryInfoList")
    public ResponseResult getInventoryInfoList() {
        List<Inventory> list = inventoryService.doGetInventoryInfoList();
        return ResponseResult.success("获取库存列表成功！", list);
    }

    public ResponseResult fallbackGetInventoryInfoList(Throwable throwable) {
        System.err.println("断路器触发 - 获取库存列表失败: " + throwable.getMessage());
        return ResponseResult.success("系统繁忙，返回部分数据", Collections.emptyList());
    }

    /**
     * 查询库存列表（带条件）
     */
    @GetMapping("/query.do")
    @CircuitBreaker(name = "inventoryService", fallbackMethod = "fallbackQueryInventoryInfoList")
    public ResponseResult queryInventoryInfoList(Inventory inventory) {
        List<Inventory> list = inventoryService.doQueryInventoryInfoList(inventory);
        return ResponseResult.success("查询库存列表成功！", list);
    }

    public ResponseResult fallbackQueryInventoryInfoList(Inventory inventory, Throwable throwable) {
        System.err.println("断路器触发 - 查询库存列表失败: " + throwable.getMessage());
        return ResponseResult.success("系统繁忙，返回部分数据", Collections.emptyList());
    }
}